import {
  Piece
} from "./piece"

export class ChessBoard {
  // 棋盘类
  init() {
    this.first = true
    this.checkTextStyle = {
      color: '#000000',
      fontSize: 14
    }
    this.checkNum = 15

    this.group = this.scene.add.group()
    this.reset()
    this.mark = false
  }
  constructor(scene) {
    this.scene = scene
    this.gameCenterX = this.scene.gameCenterX
    this.gameCenterY = this.scene.gameCenterY
    this.init()
  }
  // 重置棋盘
  reset() {
    this.player = 1
    // 15 X 15 棋盘 null未下 piece棋子类
    this.checks = []
    for (let i = 0; i < this.checkNum; i++) {
      this.checks.push([])
      for (let j = 0; j < this.checkNum; j++) this.checks[i].push(null)
    }
    this.records = []
    this.group.clear(true, true)
  }
  // 加载联机数据
  loadData(data) {
    for (let item of data.record) {
      var [name, row, col] = item
      this.createPiece(name, row, col, false) // 加载棋子不记录
    }
  }
  // 绘制棋盘 
  draw() {
    // 棋盘背景与棋子大小
    this.checkBg = this.scene.add.image(this.gameCenterX, this.gameCenterY, 'checkerBoardBg')
    this.cellW = this.checkBg.width / (this.checkNum + 1)
    this.cellH = this.checkBg.height / (this.checkNum + 1)
    this._drawGridLine()
    this._drawCheckText()
  }
  // 绘制网格线
  _drawGridLine() {
    this.grid = this.scene.add.grid(
      this.checkBg.x, this.checkBg.y,
      this.checkBg.width - this.cellW * 2, this.checkBg.height - this.cellH * 2,
      this.cellW, this.cellH,
      0x000, 0,
      0x000000, 0.2
    )
    let border = this.scene.add.graphics()
    border.lineStyle(3, 0x000000, 0.5)
    border.strokeRoundedRect(
      (this.scene.gameWidth - this.checkBg.width) / 2 + this.cellW,
      (this.scene.gameHeight - this.checkBg.height) / 2 + this.cellH,
      this.checkBg.width - this.cellW * 2, this.checkBg.height - this.cellH * 2,
      5
    )
    this.grid.setInteractive()
    this.grid.on('pointerdown', (e) => {
      if (this.scene.isAllowCheck()) {
        this._computedPosition(e.x, e.y)
      }
    })
  }

  // 绘制标志
  _drawCheckText() {
    let bgX = this.scene.computedX(this.checkBg.width)
    let bgY = this.scene.computedY(-this.checkBg.height) - this.cellH
    let X = bgX + 5
    let Y = bgY - 5
    for (let i = 1; i <= this.checkNum; i++) {
      this.scene.add.text(X, Y, i, this.checkTextStyle)
      Y -= this.cellH
    }
    X = bgX + this.cellW - 5
    Y = bgY + 10
    for (let i = 65; i <= this.checkNum + 65 - 1; i++) {
      let str = String.fromCharCode(i)
      this.scene.add.text(X, Y, str, this.checkTextStyle)
      X += this.cellW
    }
    // 绘制天元
    this.scene.add.ellipse(bgX + this.cellW * 4, bgY - this.cellH * (this.checkNum - 4), 10, 10, 0x666666, 1)
    this.scene.add.ellipse(bgX + this.cellW * (this.checkNum - 3), bgY - this.cellH * (this.checkNum - 4), 10, 10, 0x666666, 1)
    this.scene.add.ellipse(bgX + this.cellW * 4, bgY - this.cellH * 3, 10, 10, 0x666666, 1)
    this.scene.add.ellipse(bgX + this.cellW * (this.checkNum - 3), bgY - this.cellH * 3, 10, 10, 0x666666, 1)
    this.scene.add.ellipse(bgX + this.cellW * (1 + Math.floor(this.checkNum / 2)), bgY - this.cellH * (Math.floor(this.checkNum / 2)), 10, 10, 0x666666, 1)
  }
  // 计算落子方位
  _computedPosition(x, y) {
    let sX = this.scene.computedX(this.checkBg.width) + this.cellW
    let sY = this.scene.computedY(this.checkBg.height) + this.cellH

    let row, col;
    // 计算位置
    if (x < sX + this.cellW / 2) row = 0
    else if (x > sX + this.cellW * 14.5) row = 14
    else row = Math.floor((x - sX - this.cellW / 2) / this.cellW) + 1

    if (y < sY + this.cellH / 2) col = 0
    else if (y > sY + this.cellH * 14.5) col = 14
    else col = Math.floor((y - sY - this.cellH / 2) / this.cellH) + 1

    this.createPiece(this.scene.isBlack, row, col)
  }
  // 生成棋子并返回
  createPiece(name, row, col, record = true) {
    if (this.checks[row][col]) return

    // if (this.first) {
    //   if (!(row == 7 && col == 7)) return this.scene.firstBlackCheck()
    //   this.first = false
    // }
    let sX = this.scene.computedX(this.checkBg.width) + this.cellW
    let sY = this.scene.computedY(this.checkBg.height) + this.cellH

    let x = sX + this.cellW * row
    let y = sY + this.cellH * col

    let n = name == 1 ? 'pieceBlack' : 'pieceWhite'
    let piece = new Piece(this.scene, x, y, n, this, row, col,this.records.length+1)
    if (this.mark) piece.sizeText.setVisible(true)
    return this._recordCheck(name, piece, record)
  }
  // 记录棋子
  _recordCheck(name, piece, record) {
    let row = piece.getData('row')
    let col = piece.getData('col')
    this.checks[row][col] = piece
    this.records.push([name, row, col])
    this.group.add(piece)

    this._check(piece)
    // this._changePlayer(record) // 交换棋手
    if (record) this.scene.chessAfter()
  }
  // 交换棋手
  _changePlayer(record) {
    if (this.scene.status == 0) {
      this.player = this.player ? 0 : 1
      if (record) this.scene.chessAfter()
    }
  }
  showMark() {
    this.mark = !this.mark
    this.group.children.iterate(function (child) {
      // console.log(child)
      if (this.mark) child.sizeText.setVisible(true)
      else child.sizeText.setVisible(false)
    }, this)
  }
  // 检查胜利条件
  _check(piece) {
    // for (let [r1,c1,r2,c2] of [[-1, 0, 1, 0],[0, -1, 0, 1],[-1, -1, 1, 1]])
    for (let [
        r1,
        c1,
        r2,
        c2
      ] of [
        [-1, 0, 1, 0],
        [0, -1, 0, 1],
        [-1, -1, 1, 1],
        [1, -1, -1, 1]
      ]) {
      if (1 + this._find(r1, c1, piece) + this._find(r2, c2, piece) >= 5) {
        return this.scene.Victory(piece)
      }
    }
  }
  // 递归搜寻某方向上的同色棋子
  _find(row, col, piece, count = 0) {
    let r = piece.getData('row') + row
    let c = piece.getData('col') + col
    if (r < 0 || c < 0 || r >= this.checkNum || c >= this.checkNum) return count
    if (this.checks[r][c] == null || this.checks[r][c].key != piece.key) return count
    return this._find(row, col, this.checks[r][c], count + 1)
  }
}
